package com.kaijie.scheduled;

import com.kaijie.contants.SmtDeviceTypeEnum;
import com.kaijie.contants.TableNameEnum;
import com.kaijie.entity.CloudExeLog;
import com.kaijie.entity.CloudPcbinfom;
import com.kaijie.service.CloudExeLogService;
import com.kaijie.service.CloudPcbinfomService;
import com.kaijie.service.PcbInfomService;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author pxh
 * @TODO PcbinfoD表定时任务
 * @Date 2017/7/3
 */
@Component
public class PcbinfomScheduled {

    @Resource
    @Qualifier("fristSqlSessionTemplate")
    private SqlSessionTemplate fristSqlSessionTemplate;

    @Resource
    @Qualifier("secondSqlSessionTemplate")
    private SqlSessionTemplate secondSqlSessionTemplate;

    @Resource
    @Qualifier("thirdSqlSessionTemplate")
    private SqlSessionTemplate thirdSqlSessionTemplate;

    @Resource
    private CloudExeLogService cloudExeLogService;

    @Resource
    private CloudPcbinfomService cloudPcbinfomService;

    @Resource
    private PcbInfomService pcbInfomService;

    private static final Logger LOGGER = LoggerFactory.getLogger(PcbinfomScheduled.class);

    @Scheduled(cron = "*/30 * * * * ? ")
    @PostConstruct
    public void queryAndSavePcbinfod() {
        Map<String, Object> param = new HashMap();
        param.put("tableName", TableNameEnum.PCBINFOM);
        List<CloudExeLog> cloudExeLogList = cloudExeLogService.findByTableName(param);
        for (CloudExeLog cloudExeLog : cloudExeLogList) {
            findPcbinfodTable(cloudExeLog);
        }
    }

    /**
     * 查询第三方库
     *
     * @param cloudExeLog
     */
    private void findPcbinfodTable(CloudExeLog cloudExeLog) {
        for (SmtDeviceTypeEnum smtType : SmtDeviceTypeEnum.values()) {
            //判断数据来源是否一致
            if (smtType.toString().equalsIgnoreCase(cloudExeLog.getDeviceId())) {
                queryThirdPartyDb(smtType, cloudExeLog);
            }
        }
    }

    /**
     * 判断使用那个数据源
     *
     * @param smtType
     * @param cloudExeLog
     */
    private void queryThirdPartyDb(SmtDeviceTypeEnum smtType, CloudExeLog cloudExeLog) {
        switch (smtType) {
            case FRIST:
                queryAndSaveData(cloudExeLog, fristSqlSessionTemplate);
                break;
            case SECOND:
                queryAndSaveData(cloudExeLog, secondSqlSessionTemplate);
                break;
            case THIRD:
                queryAndSaveData(cloudExeLog, thirdSqlSessionTemplate);
                break;
            default:
                break;
        }
    }

    /***
     * 查询第三方数据库并保持道本地数据库
     * @param cloudExeLog
     * @param sqlSessionTemplate
     */
    private void queryAndSaveData(CloudExeLog cloudExeLog, SqlSessionTemplate sqlSessionTemplate) {
        List<CloudPcbinfom> cloudPcbinfods;
        try {
            cloudPcbinfods = pcbInfomService.findByPcbId(sqlSessionTemplate, cloudExeLog.getStartTime());
            if (cloudPcbinfods != null && !cloudPcbinfods.isEmpty()) {
                for (CloudPcbinfom cloudPcbinfom : cloudPcbinfods) {
                    cloudPcbinfom.setCreateTime(new Date());
                    cloudPcbinfom.setDeviceId(cloudExeLog.getDeviceId());
                    cloudPcbinfomService.save(cloudPcbinfom);
                }
                //成功
                cloudExeLog.setResult(0);
                cloudExeLog.setStartTime(new Date());
                cloudExeLogService.update(cloudExeLog);
            }
        } catch (Exception e) {
            LOGGER.error("query " + cloudExeLog.getDeviceId() + " db error", e);
        }
    }

}
